package com.htgd.gardenexpo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConSocialTopics;
import com.htgd.gardenexpo.dao.GeConSocialTopicsMapper;
import com.htgd.gardenexpo.page.SocialTopicsQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.IGeConSocialTopicsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 社交话题 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-08
 */
@Service
public class GeConSocialTopicsServiceImpl extends ServiceImpl<GeConSocialTopicsMapper, GeConSocialTopics> implements IGeConSocialTopicsService {

    @Autowired
    private GeConSocialTopicsMapper geConSocialTopicsMapper;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @Override
    public PageInfo<GeConSocialTopicsVO> pageList(SocialTopicsQuery socialTopicsQuery) {
        PageHelper.startPage(socialTopicsQuery.getPageNum(), socialTopicsQuery.getPageSize());
        List<GeConSocialTopicsVO> list= geConSocialTopicsMapper.pageList(socialTopicsQuery);
        for (GeConSocialTopicsVO geConSocialTopicsVO : list) {

            if(geConSocialTopicsVO.getType()!=null){//类型
                geConSocialTopicsVO.setTypeText(geBaseDictService.getTextByCodeAndValue("type_social_topics",geConSocialTopicsVO.getType()));
            }

            if(geConSocialTopicsVO.getStatus()!=null){//状态
                geConSocialTopicsVO.setStatusText(geBaseDictService.getTextByCodeAndValue("status2",geConSocialTopicsVO.getStatus()));
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public Boolean verifyName(String name) {
        GeConSocialTopics geConSocialTopics = geConSocialTopicsMapper.selectOne(
                new QueryWrapper<GeConSocialTopics>().eq("name",name).eq("status",1));
        if(Objects.isNull(geConSocialTopics)){
            return false;
        }
        return true;
    }

    @Override
    public CommonResult<SocialTopicNumsVO> getTopicNumsAndReplyNums(String date, Integer pageNum, Integer pageSize) {
        SocialTopicNumsVO result = new SocialTopicNumsVO();

//        DateTimeFormatter formatter =  DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        //不传，默认当天
//        if(date==null||"".equals(date)){
//            date = LocalDateTime.now().format(formatter);
//        }

        //获取各个类别下话题数
        long commonTopicNums = geConSocialTopicsMapper.getCategoryTopicNums("2");
        long sportTopicNums = geConSocialTopicsMapper.getCategoryTopicNums("1");
        result.setCommonTopicNums(commonTopicNums);
        result.setSportTopicNums(sportTopicNums);

        //获取各个话题下面评论数
        PageHelper.startPage(pageNum,pageSize);
        List<SocialTopicReplyNumsVO> topicReplyNumsVOS = geConSocialTopicsMapper.getTopicReplyNums(date,pageNum,pageSize);
        result.setTopicReplyNums(topicReplyNumsVOS);

        return CommonResult.success("查询成功",result);
    }
}
